﻿/* This depicts a Gaussian pulse encountering a join between two strings of different thickness. 
The strings are at the same tension, so the differing thicknesses mean different speeds of sound 
in the two strings. The variable r below is the ratio of the speed of sound in the right string 
to that in the left.

wave_1 below is the incoming Gaussian pulse from the left. 

wave_2 is an incoming Gaussian pulse from the right: it has the same apread and speed as wave_1, 
but different amplitude. It is NOT a solution to the wave equation for the right hand string
(wrong speed) but we only add in the part of it in the left-hand half, where it becomes the
reflected pulse.

wave_3 is an incoming Gaussian pulse from the left, having spread r times that of wave_1 
(and wave_2) and speed up by a factor of r.  It is therefore a solution of the wave 
equation for the right-hand string, and we only count its contribution in the right hand half.

The amplitudes of wave_2 and wave_3 are determined by the requirement that at the join, 
the two strings must have the same y-displacement and the same slope. The position of the 
left-hand string is wave_1 + wave_2, that of the right-hand string is wave_3.
*/

package {
	import flash.display.Sprite;
	import flash.events.Event;
//	import flash.display.StageAlign;
//	import flash.display.StageScaleMode;
	[SWF(width="636", height="464")]

	public class SpeedMismatch extends Sprite {


		public var center_init: Number;
		public var center: Number;
		public var spread: Number;
		public var color: uint;
		private var wave_x: Number;
		private var delta_x: Number;
		private var wave_y:Number;
		private var wave_1_y:Number;
		private var wave_2_y:Number;
		private var wave_3_y:Number;
		private var reflector: Number;
		private var speed: Number = 3;
		private var r: Number = 2;//speed ratio
		private var mul: Number; // line thickness
		private var mur: Number; // line thickness
		private var i: uint = 0;
		public var controlpanel:ControlPanel;
		private var speedslider:HorizontalSlider;


		public function SpeedMismatch(center:Number = 100, spread:Number = 20, color:uint=0xff0000) {
			this.center_init = center_init;
			this.center = center;
			this.spread = spread;
			//ratio of speeds before and after join:
			this.r = r;
			this.color = color;
			init();
		}
		public function init():void {

			spread = 30;
			r = .1;//speed ratio
			center_init = 0.15*stage.stageWidth;
			center = center_init;
			wave_x = 10;
			wave_1_y = 0;
			wave_2_y = 0;
			wave_3_y = 0;

			delta_x = 0.5;
			reflector = 0.5*stage.stageWidth;
			controlpanel = new ControlPanel(stage.stageHeight, stage.stageWidth);
			addChild(controlpanel);
			speedslider = new HorizontalSlider(0.1,1.9,1,.05,false);
			speedslider.x = .3*stage.stageWidth;
			speedslider.y = .91*stage.stageHeight;
			speedslider.height=.077*stage.stageHeight;
			speedslider.value = 1;
			addChild(speedslider);

			addEventListener(Event.ENTER_FRAME, onEnterFrame);

		}
		public function onEnterFrame(event:Event):void {

			if(speedslider.value <= 1){
				r = speedslider.value;
				mul = Math.ceil(r);
				mur = Math.floor(11-10*r);
			}
			else{
				r = 10*(speedslider.value-1)+1;
				mul = Math.floor(r);
				mur = Math.ceil(r/10);
			}
			if (center < 2*reflector - center_init) {
				if (!controlpanel.pause) {
					center += speed;
				}
				if (controlpanel.stepback) {
					center -= speed/2;
				}
				if (controlpanel.stepforward) {
					center += speed/2;
				}
				if (controlpanel.reset) {
					center = center_init;
					wave_x = 10;
					controlpanel.pause = true;
				}

				graphics.clear();
				MismatchWaveDraw();

			} else {
				controlpanel.pause = true;
				if (controlpanel.reset) {
					center = center_init;
					wave_x = 10;
				}
			}
		}
		public function MismatchWaveDraw():void {

			graphics.lineStyle(1, 0x000000, 0);
			graphics.moveTo(reflector, 200);
			graphics.lineTo(0, 200);


			for (i = 0; i < (2*reflector/delta_x); i++) {
				wave_x =  i*delta_x;
				wave_y = 200 + wave_1_y + wave_2_y + + wave_3_y;

				wave_1_y = -(50*Math.exp(-((wave_x - center)*(wave_x - center)/(spread*spread))));
				wave_2_y = -(50*((r-1)/(r+1))*Math.exp(-((wave_x - (2*reflector - center))
				*(wave_x - (2*reflector - center))/(spread*spread))));

				wave_3_y = -(50*(2*r/(1+r))
				*Math.exp(-((wave_x - ((1-r)*reflector+r*center))
				*(wave_x - ((1-r)*reflector +r*center))/(r*r*spread*spread))));


				if (i < (reflector/delta_x)) {
					graphics.lineStyle(mul+1, 0x000000, 1);

					wave_y = 200 + wave_1_y + wave_2_y;
				} else {

					graphics.lineStyle(mur+1, 0xff0000, 1);

					wave_y = 200 + wave_3_y;
				}
				graphics.lineTo(wave_x, wave_y);
			}
			graphics.moveTo(0,200);

		}
	}
}